package com.virus.mall.controller.admin;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.virus.mall.model.Admin;
import com.virus.mall.model.Result;
import com.virus.mall.model.bo.*;
import com.virus.mall.model.vo.AdminLoginVo;
import com.virus.mall.service.AdminService;
import com.virus.mall.service.impl.AdminServiceImpl;
import com.virus.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Classname AdminServlet
 * @Description NULL
 * @Date 2021/4/12 15:46
 * @Created by virus
 */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {

    private AdminService adminService = new AdminServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/admin/", "");
        if (op.equals("login")) {
            login(request, response);
        } else if (op.equals("getSearchAdmins")) {
            getSearchAdmins(request, response);
        } else if (op.equals("changePwd")) {
            changePwd(request, response);
        } else if (op.equals("logoutAdmin")) {
            logoutAdmin(request, response);
        } else if (op.equals("addAdminss")) {
            addAdminss(request, response);
        } else if (op.equals("updateAdminss")) {
            updateAdminss(request, response);
        }
    }

    /**
     * 管理员模块登录
     * @param request
     * @param response
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminLoginBo adminLoginBo = objectMapper.readValue(requestBody, AdminLoginBo.class);
        if (!adminLoginBo.valid()) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("用户名密码不能为空")));
            return;
        }
        int login = adminService.login(adminLoginBo);
        if (login == 200) {
            AdminLoginVo adminLoginVo = new AdminLoginVo(adminLoginBo.getEmail(), adminLoginBo.getEmail());
            HttpSession session = request.getSession();
            session.setAttribute("email", adminLoginVo.getToken());
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok(adminLoginVo)));
            return;
        }
//        response.setStatus(login);
        response.getWriter().println(objectMapper.writeValueAsString(Result.error("用户名密码不正确")));
    }

    /**
     * 模糊查询管理员
     * @param request
     * @param response
     * @throws IOException
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminSearchBo adminSearchBo = objectMapper.readValue(requestBody, AdminSearchBo.class);
        List<Admin> adminList = adminService.getSearchAdmins(adminSearchBo);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(adminList)));
    }

    /**
     * 更改管理员密码
     * @param request
     * @param response
     * @throws IOException
     */
    private void changePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminChangeBo adminChangeBo = objectMapper.readValue(requestBody, AdminChangeBo.class);
        if (adminChangeBo.getNewPwd() == null || adminChangeBo.getNewPwd().equals("")) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("新密码不能为空")));
            return;
        }
        boolean result = adminService.changePwd(adminChangeBo);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("修改密码未成功")));
        }
    }

    /**
     * 登出管理员账号
     * @param request
     * @param response
     * @throws IOException
     */
    private void logoutAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminLogoutBo adminLogoutBo = objectMapper.readValue(requestBody, AdminLogoutBo.class);
        //TODO 注销逻辑
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
    }

    /**
     * 增加管理员账号
     * @param request
     * @param response
     * @throws IOException
     */
    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminAddBo adminAddBo = objectMapper.readValue(requestBody, AdminAddBo.class);
        boolean result = adminService.addAdminss(adminAddBo);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        }else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("未增加管理员成功")));
        }
    }

    /**
     * 更新管理员信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminUpdateBo adminUpdateBo = objectMapper.readValue(requestBody, AdminUpdateBo.class);
        boolean result = adminService.updateAdminss(adminUpdateBo);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        }else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("更新管理员信息未成功")));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/admin/", "");
        if (op.equals("allAdmins")) {
            allAdmins(request, response);
        } else if (op.startsWith("getAdminsInfo")) {
            getAdminsInfo(request, response);
        } else if (op.startsWith("deleteAdmins")) {
            deleteAdmins(request, response);
        }
    }

    /**
     * 获取所有管理员
     * @param request
     * @param response
     * @throws IOException
     */
    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> admins = adminService.allAdmins();
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(admins)));
    }

    /**
     * 获得管理员信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Admin admin = adminService.getAdminsInfo(id);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(admin)));
    }

    /**
     * 删除某管理员账号
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        boolean result = adminService.deleteAdmins(id);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("删除未成功")));
        }
    }
}
